<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?
require_once  '../classes/ConnectionDB.php';
require_once  '../classes/UtenteManager.php';
require_once  '../classes/TestDataManager.php';
require_once  '../bean/Tests.php';
$connection =ConnectionDB::getInstance()->connectToDB();
$codiceUtente = $_COOKIE['codiceUtente'];
$result = UtenteManager::getInstance()->getUtente($connection, $codiceUtente);

$argomenti =TestDataManager::getInstance()->getArgomenti($connection, $codiceUtente);




?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Report test</title>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/prototype.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/glassbox/glassbox.js" type="text/javascript"></script>
        <style type="text/css">
            @import url("../css/style.css");
            table{
                width: 100%;
            }

            #tableResults td{
                text-align: center;
            }


            #searchForm {
                padding-top: 3%;
            }

            #searchFormCont
            {
                padding-left: 2%;
            }

            #navigator
            {
                font-size: 14px;
            }

        </style>
        <script type="text/javascript">
            path_to_root_dir = "../glassboxLatest/glassbox.2.0.0/";
            var listaDomande;
            var numItem=8;
            var currentIndex=0;

            window.onload = function()
            {
                var searchForm = new GlassBox();
                searchForm.init( 'searchForm', '640px', '190px', 'hidden','',true, false);
                searchForm.ipos();
                searchForm.appear();

                var results = new GlassBox();
                results.init( 'results', '1090px', '390px', 'auto','',true,false);
                results.ipos();
                results.appear();
            }

            function xXMLHttpRequest()
            {

                var client = null;
                if (window.XMLHttpRequest)
                {
                    client=new XMLHttpRequest();
                }
                // code for IE
                else if (window.ActiveXObject)
                {
                    client=new ActiveXObject("Microsoft.XMLHTTP");
                }

                return client;
            }

            function handler() {

                if(this.readyState == 4 && this.status == 200)
                {
                    // alert("resp : " + this.responseText);
                    var resp = eval(this.responseText);
                    listaDomande = new Array();
                    if(resp!=null)
                    {
                        for(i=0;i<resp.length; i++)
                        {
                            listaDomande[i] = resp[i];
                        }
                    }

                    manageNavi(0);
                } else if (this.readyState == 4 && this.status != 200) {
                    // fetched the wrong page or network error...
                    alert("error this.status : " + this.status );

                }
            }

            function handlerUpdate() {
                if(this.readyState == 4 && this.status == 200)
                {
                    find();
                } else if (this.readyState == 4 && this.status != 200) {
                    // fetched the wrong page or network error...
                    alert("error this.status : " + this.status );

                }
            }


            function find()
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = handler;
                var nome = document.getElementById('nome').value;

                var param = "operation=reportSottomessi&nome="+ nome;
                client.open("POST", "../report/reportTestListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);

            }



            function updateOperation()
            {
                var codiceTest = document.getElementById('codiceTest').value;
                var nomeTest = document.getElementById('nomeTest').value;
                var oreTest = document.getElementById('oreTest').value;
                var minutiTest = document.getElementById('minutiTest').value;

                var client = xXMLHttpRequest();
                client.onreadystatechange = handlerUpdate;

                var param = "operation=update"
                    +"&codiceTest="+codiceTest
                    +"&nomeTest="+nomeTest
                    +"&oreTest="+oreTest
                    +"&minutiTest="+minutiTest;

                client.open("POST", "reportTestListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);
            }

            function sottomettiTest(codiceTest)
            {
                location.href='../testManaging/liveTest.php?codiceTest='+codiceTest;
            }

            function manageNavi(ind)
            {
                var tmp= new Array();

                var str="<table border='1' id='tableResults' cellpadding='5'>"
                    +"<tr>"
                    +"<th>Codice Test</th>"
                    +"<th>Key generata</th>"
                    +"<th>Disciplina</th>"
                    +"<th>Classe</th>"
                    +"<th>Data creazione</th>"
                    +"<th>Nome</th>"
                    +"<th>Tempo</th>"
                    +"<th>Elenco Domande</th>"
                    +"<th>Stato</th>"
                    +"<th>Partecipazioni test</th>"
                    +"<th>Test pdf</th>"
                    +"<th>Risultati</th>"
                    +"<th>Elimina</th>"
                    +"</tr>";
                var indCurrenct=0;
                while(indCurrenct<numItem)
                {
                    //                    alert("idn " + ind);


                    if(ind<listaDomande.length)
                    {
                        tmp=listaDomande[ind]
                        var nome=tmp.nome;
                        var stato="";
                        if(tmp.stato=='1')
                            stato='Aperto';
                        else
                            stato='Chiuso';

                        str+="<input type='hidden' id='codiceTest' value='"+tmp.codiceTest +"'/>"+
                            "<tr>"+
                            "<td>"+tmp.codiceTest+"</td>"+
                            "<td>"+tmp.key+"</td>"+
                            "<td>"+tmp.nomeDisciplina+"</td>"+
                            "<td>"+tmp.classe+" " +tmp.sezione+ " " +tmp.descrizioneIndirizzo+"</td>"+
                            "<td>"+tmp.dataCreazione+"</td>"+
                            "<td>"+nome+"</td>"+
                            "<td>"+tmp.ore +" hh : " + tmp.minuti +" mm</td>"+
                            "<td><a href='../report/testQuestions.php?chiaveTest="+tmp.key+"&fromSottomessi=1&codiceTest="+tmp.codiceTest+"'><img src='../images/icons/PNG/Document.png' height='30' width='30' title='Elenco Domande' border='0'/></a></td>"+
                            "<td>"+stato+"</td>";
                        str +='<td><a href=\"javascript:makePdf('+tmp.codiceTest+', '+tmp.ore+', '+tmp.minuti+', \''+tmp.key+'\', '+tmp.classeForTest+');\"><img src="../images/icons/PNG/Adobe - Acrobat.png" height="30" width="30" title="Partecipazioni test" border="0"/></a></td>';
                        str +='<td><a href=\"javascript:makeTestPdf('+tmp.codiceTest+', '+tmp.ore+', '+tmp.minuti+', \''+tmp.key+'\', '+tmp.classeForTest+');\"><img src="../images/icons/PNG/Adobe - Acrobat.png" height="30" width="30" title="Test in PDF" border="0"/></a></td>';
                        str +="<td><a href='risultatiTest.php?key="+tmp.key+"&codiceTest="+tmp.codiceTest+"'><img src='../images/icons/PNG/Magnifying Glass.png' height='30' width='30' title='Risultati' border='0'/></a></td>"+
                            "<td><input type='button' value='Elimina' onclick='javascript:eliminaTest("+tmp.codiceTest+", \""+tmp.key+"\");'/></td>"
                        "</tr>";

                    }
                    ind++;
                    indCurrenct++;
                }
                str +="</table>";
                var numTotItem = listaDomande.length;
                var numPag = Math.ceil(numTotItem / numItem);
                if(numPag > 1)
                {
                    str +="<div id='navigator'><center>";
                    var current = 0;
                    var toPrint=1
                    for(k=1;k<=numPag;k++)
                    {
                        str+="<a href='javascript:manageNavi("+current+")'>"+toPrint+"</a>&nbsp;&nbsp;";
                        current= k * numItem;
                        toPrint = toPrint+1;
                    }
                    str+="</center></div>";
                }
                document.getElementById('resultsCont').innerHTML=str;
            }

            function eliminaTest(codice, key)
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = handlerUpdate;

                var param = "operation=delete"
                    +"&codiceTest="+codice
                    +"&chiaveTest="+key;

                client.open("POST", "newTestListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);
            }

            function showDetail(codice)
            {
                location.href="detailQuestion.php?codiceDomanda=" + codice;
            }

            function makePdf(codiceTest, ore, minuti, key, classe)
            {
                document.getElementById('codiceTestPdf').value=codiceTest;
                document.getElementById('orePdf').value=ore;
                document.getElementById('minutiPdf').value=minuti;
                document.getElementById('keyPdf').value=key;
                document.getElementById('classePdf').value=classe;
                document.getElementById('operationPdf').value='makePdf';

                document.getElementById('formPdf').submit();
            }

            function makeTestPdf(codiceTest, ore, minuti, key, classe)
            {
                document.getElementById('codiceTestPdf1').value=codiceTest;
                document.getElementById('orePdf1').value=ore;
                document.getElementById('minutiPdf1').value=minuti;
                document.getElementById('keyPdf1').value=key;
                document.getElementById('classePdf1').value=classe;
                document.getElementById('operationPdf1').value='makeTestPdf';

                document.getElementById('formTestPdf').submit();

                //                location.href="downloadPdfTest.php?codiceTest="+codiceTest+"&ore="+ore+"&minuti="+minuti+"&key="+key+"&classe="+classe;
            }
        </script>
    </head>
    <body>

        <form id="formPdf" method="POST" action="newTestListener.php">
            <input type="hidden" id="codiceTestPdf" name="codiceTest" value="" />
            <input type="hidden" id="nomeTestPdf" name="nomeTest" value="" />
            <input type="hidden" id="orePdf" name="ore" value="" />
            <input type="hidden" id="minutiPdf" name="minuti" value="" />
            <input type="hidden" id="keyPdf" name="key" value="" />
            <input type="hidden" id="classePdf" name="classe" value="" />
            <input type="hidden" id="operationPdf" name="operation" value="" />
        </form>


        <form id="formTestPdf" method="POST" action="newTestListener.php">
            <input type="hidden" id="codiceTestPdf1" name="codiceTest" value="" />
            <input type="hidden" id="orePdf1" name="ore" value="" />
            <input type="hidden" id="minutiPdf1" name="minuti" value="" />
            <input type="hidden" id="keyPdf1" name="key" value="" />
            <input type="hidden" id="classePdf1" name="classe" value="" />
            <input type="hidden" id="operationPdf1" name="operation" value="" />
        </form>

        <?php include '../portal/header.php';?>
        <center>
            <div id="searchForm">
                <div id="searchFormCont">
                    <center>
                        <table cellpadding="8">
                            <tr>

                                <th colspan="2" style="text-align: center">Ricerca Test</th>
                            </tr>
                            <tr>

                                <td style="text-align: center">Nome:</td>
                                <td><input type="text" id="nome" name="nome" value="" /></td>
                            </tr>

                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>
                                    <input type="button" value="Cerca" onclick="find();" />
                                </td>
                            </tr>


                        </table>
                    </center>
                </div>

            </div>
            <div id="results"><div id="resultsCont"></div></div>
        </center>
    </body>
</html>
